<?php

class Client_OpenID_System_Moodle extends Client_OpenID_System {
	
	function displayPage($title,$body,$redirect_url=null) {
		global $CFG;
		if (!$this->messages && !$body && $redirect_url) {
			// redirect somewhere interesting if there is nothing to say
			redirect($redirect_url);
		} else {			
			print_header($title,$title);
			// if ($this->messages) {
			//	notify(implode(' ',$this->messages));
			// }
			$top = '<div style="width:500px">';
			$bottom = '</div>';
			$message_display = $top.implode(' ',$this->messages);
			print($message_display.$body);
			$homelink = '<div class="homelink"><a target="'.$CFG->framename.'" href="'.$CFG->wwwroot.'/">'.get_string('home').'</a></div>';
			print($homelink);
			print($bottom);
			print_footer('none');
		}
	}
	
	function redirectWithMessages($redirect_url) {
		if ($messages) {
			$top = '<div style="width:500px">';
			$bottom = '</div>';
			redirect($redirect_url, $top.implode(' ',$this->messages).$bottom,5);
		} else {
			redirect($redirect_url);
		}
	}
	
	function login_user($user) {
		global $USER;
	    
	    // log the user in
	    
	    $user = get_complete_user_data('username', $user->username);
	    
	    update_login_count();
	    $USER = $user;
	    
	    add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID, $USER->id, 0, $USER->id);
	
	    update_user_login_times();
	    set_moodle_cookie($openid_url);
	    set_login_session_preferences();
	    reset_login_count();
		
		$this->log_location('login_user, user logged in!');
	}

	function get_userID($user) {
		return $user->id;
	}
	
	function get_username($user) {
		return $user->username;
	}
	
	function get_email($user) {
		return $user->email;
	}
	
	function set_email($userID,$email) {
		set_field('user','email',$email,'id',$userID);
	}
		
	
	function get_display_name($user) {
		return fullname($user);
	}
	
	function insert_inactive_user($username) {
		$u = new StdClass;
		$u->username = $username;
		$u->auth = 'openid';
		$u->confirmed = 0;
		insert_record('user',$u,false);
		return $this->get_existing_user($username);
	}
	
	function email_user($email,$from,$subject,$text_body='',$html_body='') {
		// $user->mailformat = 0; //force mailing to text
		$u = new StdClass;
		$u->email = $email;
		$u->firstname = '';
		$u->lastname = '';
		$this->log_var('email_user','email',$email);
		return email_to_user($u, $from, $subject, $text_body, $html_body);
	}
	
	function activate_user($user) {
		set_field('user','confirmed',1,'id',$user->id);
	}
	
	function is_banned_user($user) {
		// does not appear to be such a concept in Moodle
		return false;
	}
	
	function is_unconfirmed_user($user) {
		return ($user->confirmed == 0);
	}
	
	function get_existing_user($username) {
		return get_record('user','username', $username);
	}
	
	function get_logged_in_user() {
		global $USER;
		return $USER;
	}
	
	function valid_email($email) {
		return validate_email($email);
	}
	
	function email_exists($email) {
		return record_exists('user','email',$email);
	}
	
	function is_logged_in() {
		global $USER, $CFG;
		
		return isset($USER->loggedin) && $USER->confirmed && ($USER->site == $CFG->wwwroot) && ($USER->username != 'guest');
	}
	
	function t($string_name) {
		return get_string($string_name,'openid_client');
	}
	
	function get_config($config_name) {
		return	get_config('openid_client', $config_name);
	}
	
	function set_field($user,$field, $value) {
		$this->debug_mode_on('/home/galaxyma/public_html/error.log');
		$this->log_var('set_field','field',$field);
		$this->log_var('set_field','value',$value);
		switch ($field) {
			case 'email':
				set_field('user','email',$value,'id',$user->id);
				break;
			case 'name':
				$namelist = explode(" ",$value);
				$lastname = array_pop($namelist);
				$firstname = implode(" ",$namelist);
				if (!trim($firstname)) {
					// that did not give us two names
					// so set arbitrary ones
					$firstname = $lastname;
					$lastname = '~';
				}
				set_field('user','firstname',$firstname,'id',$user->id);
				set_field('user','lastname',$lastname,'id',$user->id);
				break;
			case 'country':
				set_field('user','country',$value,'id',$user->id);
				break;
			case 'lang':
				set_field('user','lang',$value,'id',$user->id);
				break;
		}
	}
	
	function get_field($user,$field) {
		switch ($field) {
			case 'email':
				$val = get_field('user','email','id',$user->id);
				break;
			case 'country':
				$val = get_field('user','country','id',$user->id);
				break;
			case 'lang':
				$val = get_field('user','lang','id',$user->id);
				break;
		}
		return $val;
	}
	
	function get_param($param_name) {
		return optional_param($param_name);
	}	
}

?>